<template>
  <div class="app-container">
    <div class="filter-container">
      <el-button-group>
        <el-button class="filter-item" type="primary" icon="el-icon-edit" round @click="handleCreate" />
        <el-button class="filter-item" type="primary" round @click="batchPush">
          <svg-icon icon-class="push" />
        </el-button>
      </el-button-group>
    </div>
    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        type="selection"
        width="55"
        align="center"
      />
      <el-table-column label="标题" prop="title" align="center">
        <template slot-scope="scope">
          <a :href="`${SiteSettings.siteWebUrl}/article/${scope.row.id}`" :title="scope.row.title"><span>{{ scope.row.title }}</span></a>
        </template>
      </el-table-column>
      <el-table-column label="版权" prop="origin" align="center">
        <template slot-scope="scope">
          <el-tag :type="originType[scope.row.origin]">{{ originMap[scope.row.origin] }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="图片" prop="background" align="center">
        <template slot-scope="scope">
          <img v-if="scope.row.thumb != null" style="width: 90px;height: 90px;" :src="scope.row.thumb">
        </template>
      </el-table-column>
      <el-table-column label="分类" prop="icon" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.category.name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="浏览数" prop="lookCount" align="center">
        <template slot-scope="scope">
          <i>{{ scope.row.lookCount }}</i>
        </template>
      </el-table-column>
      <el-table-column label="评论数" prop="commentCount" align="center">
        <template slot-scope="scope">
          <i>{{ scope.row.commentCount }}</i>
        </template>
      </el-table-column>
      <el-table-column label="喜欢" prop="loveCount" align="center">
        <template slot-scope="scope">
          <i>{{ scope.row.loveCount }}</i>
        </template>
      </el-table-column>
      <el-table-column label="状态" prop="status" align="center">
        <template slot-scope="{row}">
          <el-switch v-model="row.status" @change="statusChange($event, row.id, 'status')" />
        </template>
      </el-table-column>
      <el-table-column label="是否置顶" prop="top" align="center">
        <template slot-scope="{row}">
          <el-switch v-model="row.top" @change="statusChange($event, row.id, 'top')" />
        </template>
      </el-table-column>
      <el-table-column label="是否评论" prop="comment" align="center">
        <template slot-scope="{row}">
          <el-switch v-model="row.comment" @change="statusChange($event, row.id, 'comment')" />
        </template>
      </el-table-column>
      <el-table-column fixed="right" width="150px" label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <el-button type="primary" size="mini" @click="handleUpdate(row)">
            <i class="fa fa-pencil fa-fw" />
          </el-button>
          <el-button size="mini" type="danger" @click="handleDelete(row)">
            <i class="fa fa-trash-o fa-lg" />
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
  </div>
</template>
<script>
import { feachList, del, changeStatus, batchPush } from '@/api/article'
import Pagination from '@/components/Pagination'
import { MessageBox } from 'element-ui'
import { mapGetters } from 'vuex'
export default {
  name: 'ArticleList',
  components: {
    Pagination
  },
  data() {
    return {
      listLoading: false,
      tableKey: 0,
      total: 0,
      list: [],
      originMap: {
        0: '原创',
        1: '转载',
        2: '混合'
      },
      originType: {
        0: 'success',
        1: 'warning',
        2: 'info'
      },
      listQuery: {
        page: 1,
        limit: 10,
        status: null
      },
      multipleSelection: [],
      ids: []
    }
  },
  computed: {
    ...mapGetters([
      'SiteSettings'
    ])
  },
  created() {
    this.getList()
  },
  methods: {
    async getList() {
      this.listLoading = true
      const { result } = await feachList(this.listQuery)
      this.listLoading = false
      this.list = result.list
      this.total = result.total
    },
    statusChange(value, id, type) {
      changeStatus(value, id, type).then(res => {
        this.message('修改状态', 'success', res.message)
        this.handleFilter()
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleCreate() {
      this.$router.push('/article/create')
    },
    message(title, type, message) {
      this.$notify({
        title: title,
        message: message,
        type: type,
        duration: 2000
      })
    },
    handleUpdate(row) {
      this.$router.push({
        name: 'ArticleEdit',
        params: {
          id: row.id
        }
      })
    },
    handleDelete(row) {
      MessageBox.confirm(`是否确认删除【${row.title}】这条数据？`, '删除', {
        confirmButtonText: '删除',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        del(row.id).then(res => {
          if (res.code === 0) {
            this.message('删除文章', 'success', res.message)
            this.handleFilter()
          }
        })
      })
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    batchPush() {
      this.ids = []
      if (this.multipleSelection.length === 0) {
        this.$message('请选择一条记录')
        return
      }
      this.multipleSelection.forEach(article => {
        this.ids.push(article.id)
      })
      batchPush(this.ids).then(res => {
        if (res.result) {
          this.$message({
            type: 'success',
            message: '推送成功【' + res.result.success + '】条，今日推送还剩【' + res.result.remain + '】条',
            duration: 2000
          })
        } else {
          this.$message('推送失败')
        }
      })
    }
  }
}
</script>
